package com.example.picdiy;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
	Resources res ;
	Bitmap defaul_bitmap ;
	Bitmap change_bitmap;
	ColorView colorView;
	
	RelativeLayout view;
	ImageView img_pic;
	Spinner spinner;
	String type[] = new String[] { "原始", "怀旧", "模糊", "锐化" ,"倒影", "圆角","水印","底片", "浮雕"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initBitMap(R.drawable.pic);

		view = (RelativeLayout) findViewById(R.id.main_rl_view);
		
		img_pic = (ImageView) findViewById(R.id.main_img);
		img_pic.setImageBitmap(defaul_bitmap);
		
		spinner = (Spinner) findViewById(R.id.main_spinner);
		
		colorView = (ColorView) findViewById(R.id.main_colorview);
		
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
				this, android.R.layout.simple_spinner_item, type);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setSelection(0);

		spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				switch (arg2) {
				case 0://原始
					showImageview(true);
					img_pic.setImageBitmap(defaul_bitmap);
					break;
				case 1://怀旧
					showImageview(true);
					change_bitmap = ImgUtil.oldRemeber(defaul_bitmap);
					img_pic.setImageBitmap(change_bitmap);
					break;
				case 2://模糊
					showImageview(true);
					change_bitmap = ImgUtil.blurImageAmeliorate(defaul_bitmap);
					img_pic.setImageBitmap(change_bitmap);
					break;
				case 3://锐化
					showImageview(true);
					change_bitmap = ImgUtil.sharpenImageAmeliorate(defaul_bitmap);
					img_pic.setImageBitmap(change_bitmap);
					break;
				case 4://倒影
					showImageview(true);
					change_bitmap = ImgUtil.createReflectionImageWithOrigin(defaul_bitmap);
					img_pic.setImageBitmap(change_bitmap);
					break;
				case 5://圆角
					showImageview(true);
					change_bitmap = ImgUtil.getRoundedCornerBitmap(defaul_bitmap, 10);
					img_pic.setImageBitmap(change_bitmap);
					break;
					
				case 6://水印
					showImageview(true);
					Bitmap watermark_bitmap= BitmapFactory.decodeResource(res, R.drawable.shuiyin);
					change_bitmap = ImgUtil.createBitmapForWatermark(defaul_bitmap, watermark_bitmap);
					img_pic.setImageBitmap(change_bitmap);
					break;
					
				case 7://底片
					showImageview(false);
					colorView.setDrawType(Constant.INVERT);
					colorView.invalidate();
					break;
				case 8://浮雕
					showImageview(false);
					colorView.setDrawType(Constant.EMBOSS);
					colorView.invalidate();
					break;
					
				default:
					Toast.makeText(MainActivity.this, type[arg2],
							Toast.LENGTH_SHORT).show();
					break;
				}
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}
	
	private void showImageview(boolean isShow) {
		if(isShow){
			colorView.setVisibility(View.GONE);
			img_pic.setVisibility(View.VISIBLE);
		}else{
			colorView.setVisibility(View.VISIBLE);
			img_pic.setVisibility(View.GONE);
		}
	}
	
	private void initBitMap(int drawableId) {
		res = getResources();
		defaul_bitmap = BitmapFactory.decodeResource(res, drawableId);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
